<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <!-- <el-form-item label="父节点" prop="parentName">
        <el-select v-model="form.parentId" placeholder="请选择父节点">
          <el-option
            :label="form.parentName"
            style="height: auto; padding: 0"
            :value="form.parentId"
          >
            <el-tree
              ref="treeRef"
              :data="treeData"
              default-expand-all
              :props="defaultProps"
              @node-click="handleNodeClick"
            />
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item :label="translate('公司编码')" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item :label="translate('公司名称')" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <el-form-item :label="translate('公司简称')" prop="shortName">
        <el-input v-model="form.shortName" />
      </el-form-item>
      <el-form-item :label="translate('公司分类')" prop="category">
        <el-select v-model="form.category" :placeholder="translate('请选择')">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('公司性质')" prop="nature">
        <el-select v-model="form.nature" :placeholder="translate('请选择')">
          <el-option
            v-for="item in natureList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('公司法人')" prop="manager">
        <el-input v-model="form.manager" />
      </el-form-item>
      <el-form-item :label="translate('联系人')" prop="contact">
        <el-input v-model="form.contact" />
      </el-form-item>
      <el-form-item :label="translate('联系电话')" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item :label="translate('电子邮件')" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item :label="translate('传真')" prop="fax">
        <el-input v-model="form.fax" />
      </el-form-item>
      <el-form-item :label="translate('行政区域')" prop="provinceId">
        <el-cascader
          v-model="form.area"
          clearable
          filterable
          :options="areaOptions"
          :props="{ label: 'name', value: 'code' }"
        />
      </el-form-item>
      <el-form-item :label="translate('邮政编码')" prop="postalcode">
        <el-input v-model="form.Postalcode" />
      </el-form-item>
      <el-form-item :label="translate('详细地址')" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item :label="translate('公司网址')" prop="web">
        <el-input v-model="form.web" />
      </el-form-item>
      <el-form-item :label="translate('开户信息')" prop="accountInfo">
        <el-input v-model="form.accountInfo" />
      </el-form-item>
      <el-form-item :label="translate('显示顺序')" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item :label="translate('选项')" prop="order">
        <el-switch v-model="form.order" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item :label="translate('公司描述')">
        <el-input v-model="form.remark" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">{{ translate('取消') }}</el-button>
      <el-button type="primary" @click="save">
        {{ translate('确定') }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit, GetCompany } from '@/api/companyManagement'
  import { translate } from '@/i18n'
  export default defineComponent({
    name: 'CompanyManagementEdit',
    props: {
      parentId: {
        type: String,
        default: '',
      },
    },
    emits: ['fetchData'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        treeData: [],
        defaultProps: {
          children: 'ChildNodes',
          label: 'Category',
        },
        queryForm: {
          companyId: 'string',
          parentId: 'string',
        },
        form: {},
        areaOptions: [],
        options: [
          {
            value: '集团',
            label: '集团',
          },
          {
            value: '公司',
            label: '公司',
          },
          {
            value: '分公司',
            label: '分公司',
          },
          {
            value: '子公司',
            label: '子公司',
          },
        ],
        natureList: [
          {
            value: '国家机关',
            label: '国家机关',
          },
          {
            value: '建筑业',
            label: '建筑业',
          },
          {
            value: '社会服务业',
            label: '社会服务业',
          },
          {
            value: '金融业',
            label: '金融业',
          },
          {
            value: '房地产',
            label: '房地产',
          },
          {
            value: '制造业',
            label: '制造业',
          },

          {
            value: '旅游业',
            label: '旅游业',
          },
          {
            value: '其他业',
            label: '其他业',
          },
        ],
        rules: {
          parentName: [
            { required: true, trigger: 'blur', message: '请选择父节点' },
          ],
          code: [
            { required: true, trigger: 'blur', message: '请输入公司编码' },
          ],
          fullName: [
            { required: true, trigger: 'blur', message: '请输入公司名称' },
          ],
          shortName: [
            { required: true, trigger: 'blur', message: '请输入公司简称' },
          ],
          contact: [
            { required: true, trigger: 'blur', message: '请输入联系人' },
          ],
          phone: [
            { required: true, trigger: 'blur', message: '请输入联系电话' },
          ],
          email: [
            { required: true, trigger: 'blur', message: '请输入电子邮件' },
          ],
          fax: [{ required: true, trigger: 'blur', message: '请输入传值' }],
          address: [
            { required: true, trigger: 'blur', message: '请输入详细地址' },
          ],
          web: [{ required: true, trigger: 'blur', message: '请输入公司网址' }],
          accountInfo: [
            { required: true, trigger: 'blur', message: '请输入开户信息' },
          ],
          sortCode: [
            { required: true, trigger: 'blur', message: '请输入显示顺序' },
          ],
          remark: [
            { required: true, trigger: 'blur', message: '请输入公司描述' },
          ],
          category: [
            { required: true, trigger: 'blur', message: '请选择公司分类' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })

      const fetchData = async () => {
        const {
          data: { data },
        } = await GetCompany(state.queryForm)
        state.form = data
      }
      //省市区
      // const getAreaList = async () => {
      //   const {
      //     data: { list },
      //   } = await getList()
      //   state.areaOptions = list
      // }
      const handleNodeClick = (node) => {
        state.form.parentName = node.name
        state.form.parentId = node.id
      }
      const showEdit = (row, type) => {
        if (type != 'edit') {
          state.title = '添加'
          // state.form = {
          //   companyId: row ? row.CompanyId : '0',
          //   parentId: row ? row.ParentId : '0',
          // }
        } else {
          state.title = '编辑'
          state.queryForm = {
            CompanyId: row ? row.CompanyId : '0',
            parentId: row ? row.ParentId : '0',
          }
          fetchData()
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state.form.parentId = props.parentId
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetchData')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        handleNodeClick,
        showEdit,
        close,
        save,
        translate,
        // getAreaList,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  .el-form >>> .el-input {
    width: 234px !important;
  }
  .el-form-item {
    width: 380px;
  }
</style>
